/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.inventory;

import cn.ekuma.data.ui.swing.AbstractJXTablePanel;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.rabbit.spi.I_ModelPart;
import cn.ekuma.util.SwingUtil;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryApp;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.bean.Product;
import com.openbravo.bean.SharedProduct;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.panels.model.ProductTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComponent;

/**
 *
 * @author Administrator
 */
public class JSharedProducts extends AbstractJXTablePanel implements JPanelView, BeanFactoryApp,I_ModelPart<ERP_UI_Swing_Model>{
    private I_DataLogicERP dlSales;
    private List<Product> products;
    ProductTableModel tableModel;

    public JSharedProducts() {
        super();
        tableModel=new ProductTableModel();
        getJXTable().setModel(tableModel);
        JButton clearBtn=new JButton();
        clearBtn.setText(AppLocal.getIntString("buttom.removeAll")); // NOI18N
        clearBtn.setFocusable(false);
        clearBtn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        clearBtn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        this.getToolBar().add(clearBtn);
        clearBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					dlSales.clear(SharedProduct.class);
					tableModel.resert(null);
				} catch (BasicException e1) {
					 new MessageInf(e1).show(JSharedProducts.this);
				}
			}
        });
        updateUI(true);
    }



     public String getTitle() {
        return "";
    }

    public void activate() throws BasicException {
              products=dlSales.listProductByShared();
              if(products==null)
                  products=new ArrayList();
              tableModel.resert(products);
              SwingUtil.PreferredJXTableColumn(getJXTable());
    }

    public boolean deactivate() {
        return true;
    }

    public JComponent getComponent() {
        return this;
    }

    public void init(I_AppView app) throws BeanFactoryException {
        
    }

    public Object getBean() {
        return this;
    }

    @Override
    public boolean removeSelectedObjects(int[] rowIndexs) {
      List<SharedProduct> select=new ArrayList<SharedProduct>();
      List<Product> selectProduct=new ArrayList<Product>();
      for(int o:rowIndexs){
          select.add(new SharedProduct((products.get(o)).getID()));
          selectProduct.add(products.get(o));
      }
      try {
            dlSales.deleteObjs(select);
       } catch (BasicException ex) {
            new MessageInf(ex).show(this);
            return false;
       }
       tableModel.remove(selectProduct);
      return true;
    }


    @Override
    public void addObject() {
        Product product=JProductFinder.showMessage(this, dlSales);
        if(product!=null){
            try {
                dlSales.saveSharedProduct(new SharedProduct(product.getID()));
                tableModel.add(product);
            } catch (BasicException ex) {
                new MessageInf(ex).show(this);
            }
        }
    }

    @Override
    public void editObjectAt(int row) {
        
    }

	@Override
	public String getModelName() {
		return ERP_UI_Swing_Model.MODEL_NAME;
	}

	@Override
	public void setModel(ERP_UI_Swing_Model mode) {
		dlSales =mode.getDLERP();
		
	}
}
